package com.cq.controller;

import com.cq.pojo.Employee;
import com.cq.pojo.Student;
import com.cq.pojo.User;
import com.cq.service.EmployeeService;
import com.cq.service.StuService;
import com.cq.service.UserService;
import com.cq.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/student/normal")
public class UpdateController {

    @Autowired
    private UserService userService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private StuService stuService;

    @RequestMapping("/updateAdminView")
    public String updateAdminView(){
        return "update/updateAdmin";
    }

    @RequestMapping("/updateAdmin")
    public String updateAdmin(String UserId,String UserName,String password,String root)  {
        User user = new User();
        String pwd = MD5Util.md5Hash(password,UserId);
        Integer userRoot = Integer.parseInt(root.trim());
        user.setUserId(UserId);
        user.setUserName(UserName);
        user.setPassword(pwd);
        user.setRoot(userRoot);
        int result = userService.updateUser(user);
        if(result>0) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        return "redirect:/student/normal/queryAllUser";
    }

    @RequestMapping("/updateStuView")
    public String updateStuView(){
        return "update/updateStu";
    }

    @RequestMapping("/updateStu")
    public String updateStu(String Stu_Key,String Stu_Name,String Stu_Sex,
                            String Stu_Enr,String Stu_Birth,String Stu_College,
                            String Stu_Major,String Stu_Polit,String Stu_Address,
                            String Stu_Tel,String password,String dor_id,String build_id,
                            String build_name,String oldDor_id)  {
        Student student = new Student();
        String pwd = MD5Util.md5Hash(password,Stu_Key);
        Integer stuKey = Integer.parseInt(Stu_Key.trim());
        student.setStu_Key(stuKey);
        student.setStu_Name(Stu_Name);
        student.setStu_Sex(Stu_Sex);
        student.setStu_Enr(Stu_Enr);
        student.setStu_Birth(Stu_Birth);
        student.setStu_College(Stu_College);
        student.setStu_Major(Stu_Major);
        student.setStu_Polit(Stu_Polit);
        student.setStu_Address(Stu_Address);
        student.setStu_Tel(Stu_Tel);
        student.setPassword(pwd);
        int result = userService.updateOneStu(student);
        if(result>0) {
            int result1 = userService.updateStuLiveDorm(Integer.parseInt(Stu_Key),Integer.parseInt(dor_id));
            int result2 = userService.updateStuLiveBuild(Integer.parseInt(Stu_Key),Integer.parseInt(build_id));
            int result3 = userService.updateStuLiveBuildName(Integer.parseInt(build_id),build_name);
            int result4 = userService.addDormFact(Integer.parseInt(dor_id));

            int result5 = userService.deleteOneDormFact(Integer.parseInt(oldDor_id));
            if (result1>0 && result2>0 && result3>0 && result4>0 && result5>0){
                System.out.println("修改成功");
            }
        }else {
            System.out.println("修改失败");
        }
        return "redirect:/student/normal/queryAllStu";
    }

    @RequestMapping("/updateEmpView")
    public String updateEmpView(){
        return "update/updateEmp";
    }

    @RequestMapping("/updateEmp")
    public String updateEmp(String Emp_Id,String Emp_Name,String Emp_Sex,
                         String Emp_Age,String build_id,String Emp_Job,
                         String Emp_Add,String Emp_Tel,String Emp_Des,
                         String root,String password) {
        Employee employee = new Employee();
        String pwd = MD5Util.md5Hash(password,Emp_Id);
        Integer empAge = Integer.parseInt(Emp_Age.trim());
        Integer buildId = Integer.parseInt(build_id.trim());
        Integer empRoot = Integer.parseInt(root.trim());
        employee.setEmp_Id(Emp_Id);
        employee.setEmp_Name(Emp_Name);
        employee.setEmp_Sex(Emp_Sex);
        employee.setEmp_Age(empAge);
        employee.setBuild_id(buildId);
        employee.setEmp_Job(Emp_Job);
        employee.setEmp_Add(Emp_Add);
        employee.setEmp_Tel(Emp_Tel);
        employee.setEmp_Des(Emp_Des);
        employee.setRoot(empRoot);
        employee.setPassword(pwd);
        int update = userService.updateOneEmp(employee);
        if(update>0) {
            System.out.println("添加成功！");
        }
        return "redirect:/student/normal/queryAllEmp";
    }

    @RequestMapping("/stuUpdateSelfView")
    public String stuUpdateSelfView(){
        return "update/stuUpdateSelf";
    }

    @RequestMapping("/stuUpdateSelf")
    public String stuUpdateSelf(String Stu_Key,String Stu_Name,String Stu_Sex,
                            String Stu_Enr,String Stu_Birth,String Stu_College,
                            String Stu_Major,String Stu_Polit,String Stu_Address,
                            String Stu_Tel,String password)  {
        Student student = new Student();
        String pwd = MD5Util.md5Hash(password,Stu_Key);
        Integer stuKey = Integer.parseInt(Stu_Key.trim());
        student.setStu_Key(stuKey);
        student.setStu_Name(Stu_Name);
        student.setStu_Sex(Stu_Sex);
        student.setStu_Enr(Stu_Enr);
        student.setStu_Birth(Stu_Birth);
        student.setStu_College(Stu_College);
        student.setStu_Major(Stu_Major);
        student.setStu_Polit(Stu_Polit);
        student.setStu_Address(Stu_Address);
        student.setStu_Tel(Stu_Tel);
        student.setPassword(pwd);
        int result = stuService.stuUpdateSelf(student);
        if(result>0) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        return "redirect:/student/normal/basic";
    }

    @RequestMapping("/empUpdateSelfView")
    public String empUpdateSelfView(){
        return "update/empUpdateSelf";
    }

    @RequestMapping("empUpdateSelf")
    public String empUpdateSelf(String Emp_Id,String Emp_Name,String Emp_Sex,
                            String Emp_Age,String build_id,String Emp_Job,
                            String Emp_Add,String Emp_Tel,String Emp_Des,
                            String password) {
        Employee employee = new Employee();
        String pwd = MD5Util.md5Hash(password,Emp_Id);
        Integer empAge = Integer.parseInt(Emp_Age.trim());
        Integer buildId = Integer.parseInt(build_id.trim());
        employee.setEmp_Id(Emp_Id);
        employee.setEmp_Name(Emp_Name);
        employee.setEmp_Sex(Emp_Sex);
        employee.setEmp_Age(empAge);
        employee.setBuild_id(buildId);
        employee.setEmp_Job(Emp_Job);
        employee.setEmp_Add(Emp_Add);
        employee.setEmp_Tel(Emp_Tel);
        employee.setEmp_Des(Emp_Des);
        employee.setPassword(pwd);
        int update = userService.updateOneEmp(employee);
        if(update>0) {
            System.out.println("添加成功！");
        }
        return "redirect:/student/normal/basic";
    }
}
